package com.ews.onlineexchange.controller;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.validation.constraints.Size;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ManagerUser;
import com.ews.onlineexchange.model.User;
import com.ews.onlineexchange.service.EntityHome;
import com.ews.onlineexchange.service.UserSeqNoGenerator;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.util.AesUtil;

@Named
@ConversationScoped
public class SystemUserHome extends EntityHome<ManagerUser, String> {
	private static final String DEVELOPER_ROLE = "developer";
	private static final long serialVersionUID = 1L;
	

	
	@Inject
	UserSeqNoGenerator userSeqNoGenerator;
	
	@Inject
	EntityManager entityManager;
	
	private String confirmPassword;
	
	private List<String> roles = new ArrayList<>();
	
	@Override
	public ManagerUser loadInstance() {
		ManagerUser instance = super.loadInstance();
		String roleStr = instance.getRoles();
		String[] roleArray = roleStr == null ? new String[]{} : roleStr.split("、");
		for(String role : roleArray){
			if(role.equals(DEVELOPER_ROLE)) continue;;
			roles.add(role);
		}
		return instance;
	}
	@Inject
	UserService userService;
	@Override
	public String saveAndFlush() {
		ManagerUser instance = getInstance();
		instance.setAuditted(true);
		boolean managed = this.isManaged();
		User user = userService.getLoginedUser();
		updateOperator(user);
		StringBuilder roleSb = new StringBuilder();
		for(String role : roles){
			roleSb.append(role);
			roleSb.append("、");
		}
		String roleStr = "";
		if(roleSb.length() > 0)roleStr  = roleSb.substring(0,roleSb.length() - 1);
		if("wcq666".equals(instance.getUsername())){
			roleStr = roleStr + "、" + DEVELOPER_ROLE;
		}
		instance.setRoles(roleStr);
		//另存
		if(!managed)instance.setShiroPwd(userService.entryptPassword(instance.getPassword()));
		String result = super.saveAndFlush();
//		if (!managed && RESULT_SAVED.equals(result)) {
//			keycloakUserService.persistUser(getInstance(), getRoles());
//			keycloakUserService.enableUser(getInstance());
//		} else {
//			keycloakUserService.updateRoles(getInstance(), getRoles());
//			keycloakUserService.updateEmail(getInstance());
//		}
		return "SystemUserList.xhtml?faces-redirect=true&amp;includeViewParams=true&amp;firstResult=0";
	}

	@Override
	public String delete() {
		User user = userService.getLoginedUser();
		getInstance().setDelFlag(DataEntity.DEL_FLAG_DELETE);
		updateOperator(user);
		String result = super.saveAndFlush();
		if (result.equals(RESULT_SAVED)) {
//			keycloakUserService.disableUser(getInstance());
			return "SystemUserList.xhtml?faces-redirect=true&amp;includeViewParams=true&amp;firstResult=0";
		}
		return null;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	@Size(min = 1)
	public List<String> getRoles() {
		return roles;
	}

	public void setRoles(List<String> roles) {
		this.roles = roles;
	}
}
